<?php
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;


class QuestionImagesLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $questionImagesModel;

    protected $clcwUserLogic;

    public function _initialize()
    {
        $this->questionImagesModel = D('QuestionImages', 'Model');

        $this->clcwUserLogic = D('ClcwUser', 'Logic');
    }

    /**
     * 获取问题对应的图片
     *
     * @param int $questionId
     *
     * @return mixed
     */
    public function getQuestionImagesList($questionId = 0)
    {
        $condition['question_id'] = ['eq', $questionId];
        $condition['status'] = ['eq', 1];
        $cols = 'id,url';
        $list = $this->questionImagesModel->field($cols)->where($condition)->select();
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['url'] = getFullUrl($v['url']);
            }
        }
        return $list;
    }

    /**
     * 获取审核图片列表
     *
     * @param unknown $status
     * @param unknown $page
     * @param unknown $pageSize
     */
    public function getAuditImages($status, $page, $pageSize)
    {
        $condition = [
            'i.status' => $status,
            'q.status' => $status
        ];
        $total = $this->questionImagesModel->alias('i')
            ->join('clcw_question q on i.question_id = q.question_id')
            ->where($condition)->count();

        $list = $this->questionImagesModel->alias('i')
            ->field('i.id,i.url,i.question_id')
            ->join('clcw_question q on i.question_id = q.question_id')
            ->where($condition)
            ->order('i.id asc')->page($page, $pageSize)->select();

        return [
            'total' => $total,
            'list'  => $list
        ];
    }

    /**
     * 获取图片的问题ID
     *
     * @param string $ids
     */
    public function getImgQuestionId($passID)
    {
        $ids = join(',', $passID);

        $passImgList = $this->questionImagesModel->field('question_id')->where("id IN ($ids)")->select();

        $arr = [];

        foreach ($passImgList as $v) {
            $arr[] = $v['question_id'];
        }

        return array_unique($arr);
    }

    /**
     * 图片审核通过
     *
     * @param string $passID
     */
    public function passImage($passID)
    {
        $ids = join(',', $passID);

        $res = $this->questionImagesModel
            ->where("id in ({$ids})")
            ->save(
                [
                    'status'      => '1',
                    'update_time' => time()
                ]
            );

        if (count($passID) != $res) {
            return false;
        }
        return true;
    }


    /**
     * 获取违规图片的问题ID
     *
     * @param unknown $illegal
     */
    public function getIllegalImgQuestionID($illegal)
    {
        $illegalImgID = join(',', $illegal);
        //查询违规图片的问题ID
        $illegalImgList = $this->questionImagesModel
            ->field('question_id')
            ->where("id IN ($illegalImgID)")
            ->select();
        $questionIdArr = [];
        foreach ($illegalImgList as $v) {
            $questionIdArr[] = $v['question_id'];
        }
        $illegalQuestionArr = array_unique($questionIdArr);

        return $illegalQuestionArr;

    }

    /**
     * 删除违规图片
     *
     * @param unknown $illegal
     */
    public function delIllegalImg($illegal)
    {
        $illegalImgID = join(',', $illegal);
        $res = $this->questionImagesModel
            ->where("id in ({$illegalImgID})")
            ->save(
                [
                    'status'      => '0',
                    'update_time' => time()
                ]
            );
        if (count($illegal) != $res) {
            return false;
        }
        return true;
    }
}